package com.browser.settings;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.prefs.Preferences;

@Data
public class SettingsManager {
    private static final Logger logger = LoggerFactory.getLogger(SettingsManager.class);
    private final Preferences preferences;
    private static final String DEFAULT_HOMEPAGE = "https://www.csdn.net";

    public SettingsManager() {
        this.preferences = Preferences.userNodeForPackage(SettingsManager.class);
        preferences.put("homepage", DEFAULT_HOMEPAGE);
        loadDefaultSettings();
    }

    private void loadDefaultSettings() {
        setIfNotExists("search_engine", "https://www.google.com/search?q=%s");
        setIfNotExists("download_path", System.getProperty("user.home") + "/Downloads");
        setIfNotExists("enable_javascript", "true");
        setIfNotExists("enable_cookies", "true");
    }

    private void setIfNotExists(String key, String defaultValue) {
        if (preferences.get(key, null) == null) {
            preferences.put(key, defaultValue);
        }
    }

    public String getHomepage() {
        return preferences.get("homepage", DEFAULT_HOMEPAGE);
    }

    public void setHomepage(String url) {
        preferences.put("homepage", url);
        logger.info("Homepage set to: {}", url);
    }

    public String getSearchEngine() {
        return preferences.get("search_engine", "https://www.google.com/search?q=%s");
    }

    public void setSearchEngine(String url) {
        preferences.put("search_engine", url);
    }

    public String getDownloadPath() {
        return preferences.get("download_path", System.getProperty("user.home") + "/Downloads");
    }

    public void setDownloadPath(String path) {
        preferences.put("download_path", path);
    }

    public boolean isJavaScriptEnabled() {
        return preferences.getBoolean("enable_javascript", true);
    }

    public void setJavaScriptEnabled(boolean enabled) {
        preferences.putBoolean("enable_javascript", enabled);
    }

    public boolean isCookiesEnabled() {
        return preferences.getBoolean("enable_cookies", true);
    }

    public void setCookiesEnabled(boolean enabled) {
        preferences.putBoolean("enable_cookies", enabled);
    }
} 